<template>
  <div class="register-bg">
    <div class="register-container">
      <el-card class="register-card" shadow="hover">
        <template #header>
          <h2>用户注册</h2>
        </template>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username"
              placeholder="用户名"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="确认密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
          <el-form-item prop="phone">
            <el-input 
              v-model="registerForm.phone"
              placeholder="手机号码"
              prefix-icon="Iphone"
              clearable
            />
          </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="registerForm.role" placeholder="请选择角色" style="width: 100%">
              <el-option label="租客" value="tenant" />
              <el-option label="房东" value="landlord" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleRegister" class="register-btn" round>
              注册
            </el-button>
          </el-form-item>
          <div class="login-link">
            已有账号？<router-link to="/login">立即登录</router-link>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Iphone } from '@element-plus/icons-vue'
import { userApi } from '../api'

const router = useRouter()
const registerFormRef = ref(null)
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  phone: '',
  role: ''
})
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (registerForm.confirmPassword !== '') {
      registerFormRef.value?.validateField('confirmPassword')
    }
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}
const handleRegister = async () => {
  if (!registerFormRef.value) return
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const { username, password, phone, role } = registerForm
        console.log('注册请求参数:', { username, password, phone, role }) // 前端日志
        const res = await userApi.register({ username, password, phone, role })
        console.log('注册接口返回:', res) // 前端日志
        ElMessage.success(res.message || '注册成功')
        router.push('/login')
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error(error.response?.data?.message || '注册失败')
      }
    }
  })
}
</script>

<style scoped>
.register-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #e3f0ff 0%, #f5f7fa 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-card {
  width: 400px;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(64,158,255,0.16);
  animation: fadeInDown 0.7s;
}
.register-btn {
  width: 100%;
  border-radius: 24px;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
  transition: background 0.2s;
}
.register-btn:hover {
  background: #66b1ff;
}
.login-link {
  text-align: center;
  margin-top: 20px;
}
.login-link a {
  color: #409EFF;
  text-decoration: none;
  font-weight: bold;
}
@keyframes fadeInDown {
  0% { opacity: 0; transform: translateY(-40px); }
  100% { opacity: 1; transform: translateY(0); }
}
@media (max-width: 600px) {
  .register-card { width: 95vw; }
}
</style> 